#include "datapack.h"
#include "initcfg.h"

//0-订阅	  1-取消订阅	 2-发布数据	 3-消息数据
char instruction[10][50] = { "subscribe" , "nsubscribe","pub","message" };
//订阅响应
char Respondto_subscribe[] = "type:subscribe\r\nchannel:news.games 1,news.it 1\r\n\r\n";
//取消订阅响应
char Respondto_unsubscribe[] = "type:unsubscribe\r\nchannel:news.games 1,news.it 1\r\n\r\n";
//发布数据响应
char Respondto_pub[] = "type:pub\r\nchannel:news.games 1,news.it 2\r\n\r\n";
char Respondto_message[] = "type:message\r\nchannel:news.games\r\nlen:";


void initcommand() {
	memset(&StaComDamo, 0, sizeof(damoDecoder));
	StaComDamo.comdata =(char *)malloc(9216);
	memset(StaComDamo.comdata, 0, 9216);
	StaComDamo.rawbuf = (char *)malloc(9216);
	memset(StaComDamo.rawbuf, 0, 9216);
	StaComDamo.bufbody = (char *)malloc(9216);
	memset(StaComDamo.bufbody, 0, 9216);
	StaComDamo.ephbufbody = (char *)malloc(9216);
	memset(StaComDamo.ephbufbody, 0, 9216);

	StaComDamo.onPack2=onPack2con;
	StaComDamo.onPack1=onPack1con;
};
//字符串中查找子字符串	    未找到：-1  找到：返回位置
int isPresent(char *line, char *word)
{
	int i, j, status, n, m;
	n = strlen(line);
	m = strlen(word);
	for (i = 0; i <= n - m; i++)
		if (strncmp(line + i, word, m) == 0) return i;
	return -1;
}

void Apipardata(damoDecoder* pdecoder,char medata[],int r){
	for (int j = 0; j < r; j++)
	{
		int res=parsingcommand(pdecoder, medata[j]);
		if(res>0){

			memset(pdecoder->rawbuf,0,9216);
			memset(pdecoder->buftype,0,20);
			memset(pdecoder->bufchannel,0,100);
			pdecoder->buffig=0;
			pdecoder->buflen=0;
		}
	}
}

//解析命令
//return 0:需要数据  1:找到完整数据包  -1:解析失败		2:消息数据
int parsingcommand(damoDecoder* pdecoder,unsigned char data)
{
	if (pdecoder->buffig == 0)
	{
		if (pdecoder->buflen == 0) {
			if (data != 't')return 0;
		}
		pdecoder->rawbuf[pdecoder->buflen++] = data;
		if (pdecoder->buflen < 4)return 0;
		if (pdecoder->buflen == 4 && memcmp(pdecoder->rawbuf, "type", 4) != 0) {
			memset(pdecoder->rawbuf, 0, 4);
			pdecoder->buflen = 0;
			return 0;
		}
		if (memcmp(pdecoder->rawbuf+pdecoder->buflen-4,"\r\n\r\n",4)==0)
		{
			int p = isPresent(pdecoder->rawbuf, "len:");
			//获取到\r\n\r\n
			if (p < 0)
			{
				pdecoder->onPack1(pdecoder->rawbuf, pdecoder->buflen);
				sscanf(pdecoder->rawbuf,"type:%s\r\nchannel:%s\r\n\r\n",pdecoder->buftype,pdecoder->bufchannel);
				//printf("type:%s\nchannel:%s\n",pdecoder->buftype,pdecoder->bufchannel);
				//未查找到len
				return 1;
			}
			else {
				//查找到len
				sscanf(pdecoder->rawbuf,"type:%s\r\nchannel:%s\r\nlen:%d\r\n",pdecoder->buftype,pdecoder->bufchannel,&pdecoder->bufbodylen);
				//printf("type:%s\nchannel:%s\nlen:%d\n",pdecoder->buftype,pdecoder->bufchannel,pdecoder->bufbodylen);
				pdecoder->buffig = 1;
			}
		}
	}
	else {
		pdecoder->bufbody[pdecoder->tobufbodylen++] = data;
		if (pdecoder->tobufbodylen != pdecoder->bufbodylen)
			return 0;
        double stax[3]={0};
		int mjd=0; double sod=0;
		if(strcmp(pdecoder->bufchannel,"/GNSS/SKY/REQ")==0)
		{
		    printf("11111\n");
			char thisstatic[100]="";
			sscanf(pdecoder->bufbody,"%[0-9a-zA-Z./];%lf,%lf,%lf",pdecoder->bufstatic,stax,stax+1,stax+2);
			printf("3333\n");
			//printf("data:%s;%lf,%lf,%lf\n",pdecoder->bufstatic,stax[0],stax[1],stax[2]);
			memset(pdecoder->bufbody,0,pdecoder->tobufbodylen);
			pdecoder->tobufbodylen = 0;
			printf("44444\n");
		}else{
		    printf("pdecoder->bufchannel:%s,concfg.channel_req:%s\n",pdecoder->bufchannel,concfg.channel_req);
		    printf("2222\n");
            memcpy(pdecoder->ephbufbody,pdecoder->bufbody,pdecoder->tobufbodylen);
            pdecoder->ephbufbodylen=pdecoder->tobufbodylen;
		}
        //printf("onPack2:%s,%d,%lf,%lf,%lf,%lf,%lf\n",pdecoder->ephbufbody, pdecoder->tobufbodylen ,stax[0],stax[1],stax[2], mjd, sod);
        pdecoder->onPack2(pdecoder->ephbufbody, pdecoder->ephbufbodylen , stax, mjd, sod);
        //printf("onPack2-End\n");
        memset(pdecoder->bufbody, 0, pdecoder->tobufbodylen);
        //memset(pdecoder->ephbufbody, 0, pdecoder->tobufbodylen);
        pdecoder->buffig = 0;
        pdecoder->tobufbodylen = 0;
		return 2;
	}
	return 0;
}


//char instruction
//拼接命令(id,data,len)
//ID:0-订阅	  1-取消订阅	 2-发布数据	 3-消息数据
//data:ID未2时发送数据，len：数据长度
//其他模式下未使用
//return >0:成功  -1:失败
int  splicingcommand(int comid,unsigned char* channel,unsigned char* comdata,int len)
{
	memset(StaComDamo.comdata, 0, 9216);
	//引导符号
	//命令ID comid
	if (memcmp(instruction[comid], "", sizeof(instruction[comid])) == 0)
	{
		printf("error(1):instruction id=%d,Invalid instruction\n",comid);
		return -1;
	}
	sprintf(StaComDamo.comdata, "type:%s\r\nchannel:%s\r\n", instruction[comid],channel);
	int blen = 0;
	//发布数据： 长度  数据内容
	if (comid == 2)
	{
		char stlen[20] = "";
		sprintf(stlen, "len:%d\r\n\r\n", len);
		strcat(StaComDamo.comdata, stlen);
		blen = strlen(StaComDamo.comdata);
		memcpy(StaComDamo.comdata + blen, comdata,len);
		return blen+len;
	}
	else  strcat(StaComDamo.comdata, "\r\n");
	return strlen(StaComDamo.comdata);
}


int onPack2con(unsigned char* comdata, int len) {
	//测试2
	printf("onPack2->长度:%d,数据:%s\n", len, comdata);
}

int onPack1con(unsigned char* comdata,int len) {
	//测试1
	printf("onPack1->长度:%d,数据:%s\n", len, comdata);
}
